<?php
Globals::requireController('Admin');
Globals::requireTable('Circletypead');
Globals::requireTable('Circleheadnews');
Globals::requireTable('Circle');

class CircleController extends AdminController
{
    protected $CircletypeadTable;
    protected $CircleheadnewsTable;
    protected $CircleTable;
    
	public function __construct($config = null)
	{
		parent::__construct($config, true);
		$this->CircletypeadTable = new CircletypeadTable($config);
		$this->CircleheadnewsTable = new CircleheadnewsTable($config);
		$this->CircleTable = new CircleTable($config);
	}
	
	public function adlistAction()
	{
	    global $circle_type;
	    $where = array('status'=>1);
	    $count = $this->CircletypeadTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->CircletypeadTable->listPage($where, 'id ASC', $pageSize, $pageId);
	    $this->view->type_data = $circle_type;
	}
	
	public function addetailAction()
	{
	    global $circle_type;
	    
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $data = $this->CircletypeadTable->getRow($id);
	        if($data['id'] > 0){
	            $this->view->data = $data;
	        }
	    }
	    
	    $this->view->type_data = $circle_type;
	}
	
	public function doaddetailAction()
	{
	    $id = $this->getIntParam('id');
	    $data['img_url'] = $this->getParam('img_url');
	    $data['rank'] = $this->getIntParam('rank');
	    $data['link'] = $this->getParam('link');
	    $data['type'] = $this->getIntParam('type');
	    if($id > 0){
	        $this->CircletypeadTable->update($data,$id);
	    }else{
            $data['create_date'] = date('Y-m-d H:i:s');
            $data['status'] = 1;
            $this->CircletypeadTable->add($data);
	    }
	    $this->redirectToAction('adlist');
	    exit();
	}
	
	public function addeleteAction()
	{
	    $id = $this->getIntParam('id');
	    $this->CircletypeadTable->update(array('status'=>0),$id);
	    $this->redirectToAction('adlist');
	    exit();
	}
	
	public function headnewslistAction()
	{
	    $type = $this->getIntParam('type');
	    $where = array('status'=>1,'type'=>$type);
	    $count = $this->CircleheadnewsTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->CircleheadnewsTable->listPage($where, 'id ASC', $pageSize, $pageId);
	    $this->view->type = $type;
	}
	
	public function headnewsdetailAction()
	{
	    $type = $this->getIntParam('type');
	    
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $data = $this->CircleheadnewsTable->getRow($id);
	        if($data['id'] > 0){
	            $this->view->data = $data;
	        }
	    }
	    
	    $this->view->type = $type;
	}
	
	public function doheadnewsdetailAction()
	{
	    $id = $this->getIntParam('id');
	    $data['title'] = $this->getParam('title');
	    $data['rank'] = $this->getIntParam('rank');
	    $data['link'] = $this->getParam('link');
	    $data['type'] = $this->getIntParam('type');
	    if($id > 0){
	        $this->CircleheadnewsTable->update($data,$id);
	    }else{
	        $data['create_date'] = date('Y-m-d H:i:s');
	        $data['status'] = 1;
	        $this->CircleheadnewsTable->add($data);
	    }
	    $this->redirectToAction('headnewslist',array('type'=>$data['type']));
	    exit();
	}
	
	public function headnewsdeleteAction()
	{
	    $id = $this->getIntParam('id');
	    $type = $this->getIntParam('type');
	    $this->CircletypeadTable->update(array('status'=>0),$id);
	    $this->redirectToAction('headnewslist',array('type'=>$type));
	    exit();
	}
	
	
	public function listAction()
	{
	    global $circle_type;
	    $where = array('status'=>1);
	    $count = $this->CircleTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $data = $this->CircleTable->listPage($where, 'id ASC', $pageSize, $pageId);
	    $this->view->data = $data;
	    $this->view->type_data = $circle_type;
	}
	
}
